<?php
$model = new BDynamicFormModel();
$model->addField('spaceId', NULL, 'Specify space ID:');
$model->addField('spaceName', NULL, 'Space Name:');
$isSpaceExist = FALSE;
$isShared = FALSE;
$space = NULL;
$isCheckedFail = FALSE;

$usersControl = new BUIControl();
$activeOperation = Blyn::app()->getActiveOperation();
$sender = Blyn::app()->getUIManager()->getSenderControl();
$updateControl = Blyn::app()->getUIManager()->getUpdateControl();
if (!empty($_POST)) {
    $model->attributes = $_POST['BDynamicFormModel'];
}
$subBoxArr = array();
if ($control->isAjaxLinkClicked('check')) {
    $postSpaceId = $_POST['BDynamicFormModel']['spaceId'];
    $space = BSpace::findById($postSpaceId);
    if ($space instanceof BSpace) {
        $isSpaceExist = TRUE;
        $model->spaceName = $space->getName();
        $model->spaceId = $postSpaceId;
    } else {
        $isCheckedFail = TRUE;
    }
    $ActiveSpace = Blyn::app()->getActiveSpace();
    if ($ActiveSpace->getAttribute('wechat_appid') == FALSE) {
        echo '<script language="javascript">';
        echo 'alert("AppId与AppSecret不能为空，请于setupWeChat填写信息！");';
        echo '</script>';
    } elseif ($ActiveSpace->getAttribute('wechat_openid') == FALSE) {
        echo '<script language="javascript">';
        echo 'alert("粉丝为空，请先更新粉丝！");';
        echo '</script>';
    } else {
        $usersControl->addRenderView('showWechatUsers', 'showWechatUsers');
    }
}
$screen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();
$app = Blyn::app()->getActiveApp();
$list = array();
$ar = Blyn::app()->getActiveSpace()->getAttribute('config.spaceListForShareWeChatUser');
if ($ar instanceof BAttribute) {
    $list = $ar->getValue();
}
if (!is_array($list))
    $list = array();

if ($control->isAjaxLinkClicked('shareWeChatUser')) {
    $spaceId = $postSpaceId = $_POST['BDynamicFormModel']['spaceId'];
    $space = BSpace::findById($spaceId);
    $list[$space->getId()] = $space->getName();
    Blyn::app()->getActiveSpace()->addAttribute('config.spaceListForShareWeChatUser', $list);
    if (!empty($_POST)) {
        $checkedArray = $_POST['subBox'];
    }
    $ActiveSpace = Blyn::app()->getActiveSpace();
   $currentAppId =$ActiveSpace->getAttribute('wechat_appid')->getValue();
   $currentAppSecret = $ActiveSpace->getAttribute('wechat_appsecret')->getValue();
    array_push($checkedArray, $currentAppId, $currentAppSecret);
    if ($ActiveSpace->myId) {
     $ActiveSpace->addAttributeAllowAcess('share_openid', $spaceId);
     $ActiveSpace->addAttribute('share_openid', $checkedArray);
     }
    $isShared = TRUE;
    echo '<script language="javascript">';
    echo 'alert("分享成功！");';
    echo '</script>';
}
?>
<div class="row" ><?php echo $control->getAjaxLink('Update Wechat Users', array('htmlOptions' => array('class' => 'btn'))); ?>
    <?php echo $control->getAjaxLink('Show Wechat Users', array('htmlOptions' => array('class' => 'btn'))); ?>
    <?php echo $control->getAjaxLink('Group news', array('htmlOptions' => array('class' => 'btn'))); ?>
</div>
<?php
if ($control->isAjaxLinkClicked('Show Wechat Users')) {
    $space = Blyn::app()->getActiveSpace();
    if ($space->getAttribute('wechat_appid') == FALSE) {
        echo '<script language="javascript">';
        echo 'alert("AppId与AppSecret不能为空，请于setupWeChat填写信息！");';
        echo '</script>';
    } elseif ($space->getAttribute('wechat_openid') == FALSE) {
        echo '<script language="javascript">';
        echo 'alert("粉丝为空，请先更新粉丝！");';
        echo '</script>';
    } else {
        $usersControl->addRenderView('showWechatUsers', 'showWechatUsers');
    }
}
if ($control->isAjaxLinkClicked('Group news')) {
    $usersControl->addRenderView('sendGroupNews', 'sendGroupNews');
}
if ($control->isAjaxLinkClicked('show share users')) {
     $spaceId = $postSpaceId = $_POST['BDynamicFormModel']['spaceId'];
     if($spaceId){
        $ActiveSpace = Blyn::app()->getActiveSpace();
        if ($ActiveSpace->myId) {
            if($ActiveSpace->getAttribute('share_openid', array('resource' => $spaceId))){
              $attr = $ActiveSpace->getAttribute('share_openid', array('resource' => $spaceId));
              $aValue = $attr->getValue();
              $usersControl->addRenderView('showShareUsers', 'showShareUsers', array('userOpenidArray' => $aValue));
                }  else {
                    echo '<script language="javascript">';
                    echo 'alert("无分享的粉丝");';
                    echo '</script>';
                }
           }  else {
                echo '<script language="javascript">';
                echo 'alert("请在管理机构中配置该机构的Specify Unique Id");';
                echo '</script>';
           }
     }  else {
        echo '<script language="javascript">';
        echo 'alert("请输入Specify space ID");';
        echo '</script>';
     }

}
if ($sender->isAjaxLinkClicked('sendSingleMessage')) {
    $nickname = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('userName');
    $userOpenid = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('userOpenid');
    $usersControl->addRenderView('sendSingleMessage', 'sendSingleMessage', array('nick_name' => $nickname, 'openid' => $userOpenid));
}
if ($sender->isAjaxLinkClicked('sendSingleMessageToShareUsers')) {
    $nickname = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('userName');
    $userOpenid = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('userOpenid');
    $AppId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('AppId');
    $AppSecret = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('AppSecret');
    $usersControl->addRenderView('sendSingleMessageToShareUsers', 'sendSingleMessageToShareUsers', array('nick_name' => $nickname, 'openid' => $userOpenid,'AppId'=>$AppId,'AppSecret'=>$AppSecret));
}
?>
<div class="form">
    <?php echo CHtml::beginForm(); ?>
    <div>
        <?php $usersControl->render(); ?>
    </div>
    <div id="share">
        <?php echo CHtml::errorSummary($model); ?>
        <?php if ($list != NULL) { ?>
            <legend>Shared Space List</legend>
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>机构ID</th>
                        <th>机构名称</th>
                        <th>操作</th>
                    </tr> 
                </thead>
                <tbody>
                    <?php
                    foreach ($list as $spaceId => $spaceName) {
                        $space = new BSpace($spaceId);
                        ?>
                    <td> <?php echo $space->myId; ?> </td>
                    <td> <?php echo $spaceName; ?> </td>
                    <td> 
                        <?php
                        echo $control->getAjaxLink(array('name' => '修改', 'id' => 'modifyShareSetting'), array('spaceId' => $spaceId, 'htmlOptions' => array('enctype' => 'multipart/form-data')));
                        ?> 
                        <?php
                        echo $control->getAjaxLink(array('name' => '删除', 'id' => 'modifyShareSetting'), array('spaceId' => $spaceId, 'htmlOptions' => array('enctype' => 'multipart/form-data')));
                        ?>
                    </td>
                <?php }
                ?>
                </tbody>
            </table>
        <?php } ?>

        <div class="row"><legend>Add New Share Space</legend></div>

        <div class="row">
            <?php echo CHtml::activeLabel($model, 'spaceId'); ?>
            <?php echo CHtml::activeTextField($model, 'spaceId'); ?>       
            <?php echo $control->getAjaxLink(array('name' => yii::t('site', 'check to share'), 'id' => 'check'), array('htmlOptions' => array('class' => 'btn'))); ?>   
            <?php echo $control->getAjaxLink(array('name' => yii::t('site', 'show share users'), 'id' => 'show share users'), array('htmlOptions' => array('class' => 'btn'))); ?>  
            <?php // echo $control->getAjaxLink(array('name' => yii::t('site', 'TEST Add SHARE ATTRIBUTE'), 'id' => 'testAddSharedAttribute'), array('htmlOptions' => array('class' => 'btn'))); ?>   
            <?php // echo $control->getAjaxLink(array('name' => yii::t('site', 'TEST SHARE ATTRIBUTE'), 'id' => 'testSharedAttribute'), array('htmlOptions' => array('class' => 'btn'))); ?>   
        </div>
        <?php
//        if ($control->isAjaxLinkClicked('testAddSharedAttribute')) {
//            $ActiveSpace = Blyn::app()->getActiveSpace();
//            if ($ActiveSpace->myId == 'xsjbh@cq.cn') {
//                $testAttribute = array('sender' => 'jianfeng');
//                $ActiveSpace->addAttributeAllowAcess('attributeSender', 'cqss@cq.cn');
//                $ActiveSpace->addAttribute('attributeSender', $testAttribute);
//            }
            ?>
            <!--<div class="text-error">Shared Attribute successfully!</div>-->
        <?php // } ?>
        <?php
//        if ($control->isAjaxLinkClicked('testSharedAttribute')) {
//            $ActiveSpace = Blyn::app()->getActiveSpace();
//            if ($ActiveSpace->myId == 'cqss@cq.cn') {
//                $attr = $ActiveSpace->getAttribute('attributeSender', array('resource' => 'xsjbh@cq.cn'));
//                $aValue = $attr->getValue();
//            }
            ?>
            <!--<div class="text-error">Sender:<?php // echo ' ' . $aValue['sender']; ?></div>-->
        <?php // } ?>   
        <?php if ($isCheckedFail) { ?>
            <div class="text-error">Space not exist!</div>
        <?php } ?>
        <?php if ($isSpaceExist) { ?>
            <div class="row">
                <?php echo CHtml::activeLabel($model, 'spaceName'); ?>
                <?php echo CHtml::activeTextField($model, 'spaceName', array('disabled' => TRUE)) ?>
            </div>  
            <div class="row submit">
                <label>Allow share your wechat users to specify space, then your new wechat user will automatically share in specify space!</label>
                <?php echo $control->getAjaxLink(array('name' => yii::t('site', 'Share Now!'), 'id' => 'shareWeChatUser'), array('htmlOptions' => array('class' => 'btn'))); ?>       
            </div>  
        <?php } ?>
    </div>
    <?php echo CHtml::endForm(); ?>
</div><!-- form -->

